package com.otaku.member.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.otaku.common.ResultUtils.ResultModel;
import com.otaku.common.ResultUtils.ResultCode;
import com.otaku.common.ResultUtils.ResultUtil;
import com.otaku.member.SysException.MyException;
import com.otaku.member.dao.MemberDao;
import com.otaku.member.dao.entity.MemberEntity;
import com.otaku.memberAPI.entity.UserEntity;
import com.otaku.memberAPI.service.IMemberService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


@Api("客户服务中心")
@RestController
public class MemberServiceImpl implements IMemberService {

    private final static Logger LOGGER = LoggerFactory.getLogger(MemberServiceImpl.class);


    @Autowired
    private MemberDao memberDao;

    @RequestMapping("/getMember")
    @Override
    public UserEntity getMember(String name) {
        UserEntity userEntity = new UserEntity();
        userEntity.setName(name);
        userEntity.setAge(20);
        return userEntity;
    }

    @RequestMapping("/getMemberInfo")
    @Override
    public String getMemberInfo() {
        return "member-info-server";
    }

    @RequestMapping("/getMemberInfoHystrix")
    public String getMemberInfoHystrix() {
        try {
            Thread.sleep(1500);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "member-info-server";
    }

    @RequestMapping("/getResultTest")
    public ResultModel getResultTest() {
        return ResultUtil.success();
    }

    @RequestMapping("/getResultTestError")
    public ResultModel getResultTestError() {
        return ResultUtil.error(ResultCode.AUTH_ERROR);
    }

    @RequestMapping("/getSysError")
    public ResultModel getSysError() {
        throw new RuntimeException();
    }

    private volatile boolean count = true;

    @RequestMapping("/getSysError2")
    public ResultModel getSysError2() {
        List<Object> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            MemberEntity entity = new MemberEntity();
            entity.setId(Long.valueOf(i));
            entity.setAge(10 + i * 3);
            entity.setName("Jim");
            list.add(entity);
        }
        if (count) {
            count = false;
            return ResultUtil.success(list);
        }
        count = true;
        throw new MyException(ResultCode.JSON_PARSE_ERROR);
    }


    @RequestMapping("/getByDB")
    public String getByDB() {
        List<MemberEntity> byList = memberDao.findByList();
        return JSONObject.toJSONString(ResultUtil.success(byList));
    }

    @RequestMapping("/insertDB")
    public ResultModel insertDB() {
        MemberEntity entity = new MemberEntity();
        entity.setId(1111);
        entity.setName("MEMBER");
        entity.setAge(18);
        memberDao.insert(entity);
        return ResultUtil.success();
    }

    @RequestMapping("/sendEmail")
    public String sendEmail(String mail) {
        try {
            Thread.sleep(1000);
            System.out.println("发送邮件成功：" + mail);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            JSONObject object = new JSONObject();
            object.put("data", ResultUtil.success());
            return object.toJSONString();
        }
    }

    @RequestMapping("/sendSms")
    public String sendSms(String sms) {
        try {
            Thread.sleep(1000);
            System.out.println("发送短信成功：" + sms);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            JSONObject object = new JSONObject();
            object.put("data", ResultUtil.success());
            return object.toJSONString();
        }
    }
}
